package cn.zhentao.service.impl;

import cn.zhentao.dtos.ResponseResult;
import cn.zhentao.manger.dto.LoginMangerDto;
import cn.zhentao.manger.dto.User_list_Param;
import cn.zhentao.mapper.TUserMapper;
import cn.zhentao.user.pojo.TUser;
import cn.zhentao.utils.common.AppJwtUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.zhentao.manger.pojo.SysUser;
import cn.zhentao.service.SysUserService;
import cn.zhentao.mapper.SysUserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.concurrent.TimeUnit;

/**
* @author EWEWW
* @description 针对表【sys_user】的数据库操作Service实现
* @createDate 2024-07-06 16:39:33
*/
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
    implements SysUserService{

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TUserMapper tUserMapper;

    //存入redis
    public void redisset(SysUser one){
        String key="sysUser"+one.getId();
        SysUser sysuser = (SysUser) redisTemplate.opsForValue().get(key);
        if(sysuser != null){
            return;  //不等于空说明里面有存储相同值
        }
        redisTemplate.opsForValue().set("sysUser"+one.getId(), one);  //存储管理人员信息
        redisTemplate.expire("sysUser"+one.getId(), 1, TimeUnit.HOURS);//1小时后过期
    }

    //拿出数据
    public SysUser getRedis(Integer id){
        SysUser sysuser = (SysUser) redisTemplate.opsForValue().get("sysUser" + id);
        return sysuser;
    }

    /*管理员登录*/
    @Override
    public ResponseResult login(LoginMangerDto dto) {
        //先判断参数是否为空
        if(StringUtils.isBlank(dto.getPhone()) || StringUtils.isBlank(dto.getUsername())){
            return ResponseResult.errorResult(500, "请输入手机号或密码");
        }
        SysUser one = this.getOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getPhone, dto.getPhone()));
        //判断是否为空
        if(one == null){
            return ResponseResult.okResult(500, "没有该账号哦");
        }
        //判断密码是否正确
        String salt = one.getSalt();
        String pass = dto.getUsername();
        //加密
        String password = DigestUtils.md5DigestAsHex((salt + pass).getBytes());
        if(!one.getPassword().equals(password)){
            return ResponseResult.errorResult(500, "密码不正确");
        }
        //返回令牌
        String token = AppJwtUtil.getToken(one.getId().longValue());
         redisset(one);//存入redis
        return ResponseResult.okResult(one.getId(),token);//返回管理员id和token令牌
    }

    /*返回给管理人员信息*/
    @Override
    public ResponseResult marginInfo(Integer id) {
        if(id == null){
            return ResponseResult.errorResult(500,"id参数不能为空");
        }
        SysUser redis = getRedis(id);
        return ResponseResult.okResult(redis);
    }


    /*管理员查询用户信息*/
    @Override
    public Page<TUser> userpage(User_list_Param param) {
        Page<TUser> page = new Page<>(param.getPagenum(),param.getPagesize());
        Page<TUser> list = tUserMapper.sys_user_list(page,param);
        return list;
    }


}




